package com.Controller;

import com.POJO.User;
import com.Service.UserService;
import com.Utils.JWTUtil;
import com.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/findTel")
    public R findTel(@RequestBody Map<String,String> Map_All){
        return new R(userService.UserFindTel(Map_All));
    }

    @PostMapping("/Register")
    public R Register(@RequestBody Map<String,String> Map_All){
        return new R(userService.UserRegister(Map_All)>0);
    }
    @PostMapping("/Login")
    public R Login(@RequestBody Map<String,String> Map_All){
        String tel=Map_All.get("tel");
        String pwd=Map_All.get("pwd");
        User user=userService.Login(tel,pwd);
        if (user == null) {
            //登录失败
            return new R(false, "用户名或密码不正确");
        } else {
            //登录成功
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("tel", tel);
            Object token = JWTUtil.getToken(dataMap);
            Map<String,Object> data=new HashMap<>();
            data.put("username",user.getUsername());
            data.put("tel",user.getTel());
            data.put("token",token);
            return new R(true, data, "登录成功");
        }
    }
    @PostMapping("/FindUser")
    public R FindUser(@RequestBody Map<String,String> Map_All){
        String tel =Map_All.get("tel");
        return new R(true,userService.FindUser(tel));
    }
    @PostMapping("/UseraddAddress")
    public R UseraddAddress(@RequestBody Map<String,String> Map_All){
        return new R(true,userService.UseraddAddress(Map_All));
    }
    @PostMapping("/updataHeadshot")
    public R updataHeadshot(@RequestParam("tel") String tel,@RequestPart("file")MultipartFile headshot){
        return new R(userService.updataHeadshot(tel,headshot)>0);
    }
    @PostMapping("/updataPwd")
    public R updataPwd(@RequestBody Map<String,String> Map_All){
        String tel=Map_All.get("tel");
        String pwd=Map_All.get("pwd");
        return new R(userService.updataPwd(tel,pwd)>0);
    }
    @PostMapping("/updataUsername")
    public R updataUsername(@RequestBody Map<String,String> Map_All){
        String tel=Map_All.get("tel");
        String username=Map_All.get("username");
        return new R(userService.updataUsername(tel,username)>0);
    }

}
